import { result } from "lodash";

/**
 * 获取任务信息
 */
export function getTaskInfo(id) {
  return RSocket.rr({
    route: `tasks.${id}`,
  });
}

/**
 * 最后位置
 */
export async function lastLocation(targetId) {
  return RSocket.rr({ route: 'geo.location.last', args: { targetId } });
}

/**
 * 位置查询
 */
export async function locations({ filter }) {
  return RSocket.rs({
    route: 'geo.locations',
    args: {
      filter: { targetId: 'vehicle:.+', ...filter },
      pg: { size: 1000000 },
    },
  });
}

export async function locationCount({ filter }) {
  return RSocket.rr({
    route: 'geo.locations.count',
    args: {
      filter: { targetId: 'vehicle:.+', ...filter },
    },
  });
}

export async function subscribeLocation({ filter = {}, onNext, onSubscribe }) {
  return new Promise((resolve) => RSocket.rs({
    route: 'geo.locations.subscribe',
    args: {
      filter: { targetId: 'vehicle:', ...filter },
    },
    onNext,
    onSubscribe: (subscription) => {
      resolve(subscription);
      onSubscribe?.(subscription);
    },
  }));
}

export async function getTrace(traceId) {
  return RSocket.rr({
    route: `geo.traces.${traceId}`,
  });
}

export async function traces({ filter, pg }) {
  return RSocket.rs({
    route: 'geo.traces',
    args: { filter: { targetId: 'vehicle:', ...filter }, pg },
  });
}

export async function traceCount({ filter }) {
  return RSocket.rr({
    route: 'geo.traces.count',
    args: { filter: { targetId: 'vehicle:', ...filter } },
  });
}

export async function vehicles(kind, keyword, pg) {
  const search = { keyword };
  if (kind > 0) {
    search.isService = kind === 1;
  }
  return RSocket.rs({
    route: 'vehicles',
    args: {
      search,
      pg,
    },
  });
}

export async function vehiclesCount(kind, keyword) {
  const search = { keyword };
  if (kind > 0) {
    search.isService = kind === 1;
  }
  return RSocket.rr({
    route: 'vehicles.count',
    args: {
      search,
    },
  });
}
